import time
from snowflake.client import default_client, get_guid
from faker import Faker
import base64
import os
import random
from string import ascii_letters


def random_str():
    return ''.join([random.choice(ascii_letters) for _ in range(6)])


def base642bin(path, data):
    """
    注意前缀：data:image/png;base64,
    :return:
    """
    image_bin = base64.b64decode(data)
    with open(os.path.join(path, random_str() + '.png'), 'wb+') as f:
        f.write(image_bin)


def t2d(timestamp):
    """
    时间戳转日期
    :param timestamp:
    :return:
    """
    timestamp_temp = time.localtime(timestamp)
    return time.strftime("%Y-%m-%d %H:%M:%S", timestamp_temp)


def d2t(t):
    """
    日期转时间戳
    :param t:
    :return:
    """
    datetime_temp = time.strptime(t, "%Y-%m-%d %H:%M:%S")
    return int(time.mktime(datetime_temp))


def get_snow_id() -> int:
    """
    获取雪花ID
    单列模式  直接调用了包内的单列
    :return:
    """
    return get_guid()


def random_ua() -> str:
    """
    获取随机UA
    :return:
    """
    ua = Faker()
    return ua.user_agent()


def default_headers(*args) -> dict:
    """
    默认请求头
    host, origin, referer  依次传入
    :return:
    """
    host, origin, referer = args
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'User-Agent': random_ua(),
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6",
        "Connection": "keep-alive",
        "Host": host,
        "Origin": origin,
        "Referer": referer
    }
    return headers


if __name__ == '__main__':
    pass
